package com.zhuss.product.processor;

import com.zhuss.bean.bo.product.train.BindStrategySiteBo;
import com.zhuss.bean.vo.product.train.DetailLineStrategyVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.CanSellType;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.LineStrategy;
import com.zhuss.product.db.entity.Train;
import com.zhuss.product.processor.cache.LineStrategyCache;
import com.zhuss.product.processor.cache.SiteStrategyCache;
import com.zhuss.product.processor.cache.TrainCache;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 售满和售卖
 * @author summer
 * @date 2021/1/28 12:56
 */

@Component("strategySell")
public class StrategySell extends AbstractProcessor<Object, BindStrategySiteBo> implements Processor<Object, BindStrategySiteBo> {

    @Autowired
    private LineStrategyDao lineStrategyDao;

    @Autowired
    private TrainDao trainDao;

    @Autowired
    @Qualifier("lineStrategyCache")
    private LineStrategyCache lineStrategyCache;

    @Autowired
    @Qualifier("trainCache")
    private TrainCache trainCache;

    @Autowired
    @Qualifier("siteStrategyCache")
    private SiteStrategyCache siteStrategyCache;

    @Override
    public void check(BindStrategySiteBo bo) throws DzcxException {

        if (bo.getLineStrategyId() == null || bo.getLineStrategyId() <= 0l) {
            throw new DzcxException(ExceptionCode.策略ID为空);
        }
        if (bo.getCanSell() == null|| bo.getCanSell() <=0l) {
            throw new DzcxException(ExceptionCode.售卖状态为空);
        }

        //如果can_sell=10 校验当前策略是否是10，如果是不让操作
        DetailLineStrategyVo lineStrategy = lineStrategyDao.detail(bo.getLineStrategyId());
        if (lineStrategy.getCanSell().equals(bo.getCanSell())) {
            throw new DzcxException(ExceptionCode.此数据不允许当前操作);
        }
        bo.setStrategyVo(lineStrategy);

    }

    @Override
    public Object process(BindStrategySiteBo bo) throws DzcxException {
        //如果是售卖操作，train表的can_sell=10
        LineStrategy strategy = new LineStrategy();
        BeanUtils.copyProperties(bo.getStrategyVo(), strategy);
        strategy.setCanSell(bo.getCanSell());
        lineStrategyDao.updateByPrimaryKeySelective(strategy);
        //清除策略缓存
        lineStrategyCache.clear(strategy.getLineStrategyId());
        //根据lineStrategyId更新train表can_sel
        Train train = new Train();
        train.setCanSell(bo.getCanSell());
        train.setLineStrategyId(bo.getLineStrategyId());
        train.setModifier(bo.getConcurrentUserName());
        train.setModifierId(bo.getModifierId());
        train.setModifyTime(new Date());
        trainDao.updateTrainByLineStrategyId(train);
        //查询train，清除train缓存
        List<Long> trainIdList = trainDao.listIdByLineStrategyId(strategy.getLineStrategyId());
        for (Long id : trainIdList) {
            trainCache.clear(id);
        }
        return null;
    }

    @Override
    public Object log(BindStrategySiteBo bo) throws DzcxException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.商品.key());
        if (bo.getCanSell().equals(CanSellType.售卖.key())) {
            record.setAction(LogActionEnum.车次维护售卖.key());
        } else {
            record.setAction(LogActionEnum.车次维护售满.key());
        }
        record.setTargetId(bo.getLineStrategyId());
        record.setTargetName(String.valueOf(bo.getLineStrategyId()));
        record.setContent("参数: " + JsonUtil.toJson(bo));
        record.setReason(bo.getExceptionReason());

        record.setCreatorId(bo.getConcurrentUserId());
        record.setCreator(bo.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
